// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Windows.Forms;
using svs.Mobile.BeyondMedia.Properties;
using svs.Mobile.BeyondMedia.UiCore.GDI;

namespace svs.Mobile.BeyondMedia
{
	public partial class SplashForm : Form
	{
		private Bitmap offScreen;

		public SplashForm()
		{
			InitializeComponent();
			ControlBox = false;
		}

		private void SplashForm_Load(object sender, EventArgs e)
		{
			offScreen = new Bitmap(ClientSize.Width, ClientSize.Height);
			Version v = Assembly.GetExecutingAssembly().GetName().Version;
			BeyondPodLabel.Text = string.Format("BeyondPod {0}.{1}.{2}", v.Major, v.Minor, v.Build);
		}

		public void UpdateInfoLabel(string label)
		{
			InfoLabel.Text = label;
			Refresh();
		}

		protected override void OnPaintBackground(PaintEventArgs e)
		{}

		protected override void OnPaint(PaintEventArgs e)
		{
			Image splash = Resources.Splash;

			using (var goffScreen = Graphics.FromImage(offScreen))
			{
				var hdc = goffScreen.GetHdc();
				using (var g = Gdi.FromHdc(hdc, Rectangle.Empty))
				{
					g.GradientFill(new Rectangle(0, 0, offScreen.Width, offScreen.Height),
						Color.Gainsboro,Color.GhostWhite,Gdi.FillDirection.TopToBottom);
				}
				goffScreen.ReleaseHdc(hdc);

				LicenseLabel.Top = ClientRectangle.Bottom - (LicenseLabel.Height);
				
				ContributeLabel3.Top = LicenseLabel.Top - ContributeLabel1.Height - 1;
				ContributeLabel2.Top = ContributeLabel3.Top - ContributeLabel2.Height + 1;
				ContributeLabel1.Top = ContributeLabel2.Top - ContributeLabel1.Height + 1;

				var ia = new ImageAttributes();
				ia.SetColorKey(Color.Black, Color.Black);

				int imgLeft = ClientRectangle.Left + ((ClientRectangle.Width / 2) - (splash.Width / 2));
				int imgTop = (int)((ClientRectangle.Top + ((ContributeLabel1.Top / 2) - (splash.Height / 2))) * 0.5);

				Rectangle imgRect = new Rectangle(imgLeft, imgTop, splash.Width, splash.Height);

				goffScreen.DrawImage(splash, imgRect, 0, 0, imgRect.Width, imgRect.Height, GraphicsUnit.Pixel, ia);

				BeyondPodLabel.Top = imgRect.Bottom + 10;
				InfoLabel.Top = BeyondPodLabel.Bottom + 1;


				DrawLabel(BeyondPodLabel, goffScreen);
				DrawLabel(InfoLabel, goffScreen);

				DrawLabel(ContributeLabel1, goffScreen);
				DrawLabel(ContributeLabel2, goffScreen);
				DrawLabel(ContributeLabel3, goffScreen);

				DrawLabel(LicenseLabel, goffScreen);

				e.Graphics.DrawImage(offScreen, 0, 0);
			}
		}

		private void DrawLabel(Label label, Graphics grx)
		{
			SizeF size = grx.MeasureString(label.Text, label.Font);
			int left = this.Width / 2 - (int)size.Width / 2;
			Rectangle rect = new Rectangle(left, label.Top, (int)size.Width, (int)label.Height);
			using (Brush b = new SolidBrush(label.ForeColor))
			{
				grx.DrawString(label.Text, label.Font, b, rect);
			}
		}

		
	}
}